package com.zzx.schoolfellow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzx.schoolfellow.common.api.ApiResult;
import com.zzx.schoolfellow.mapper.BmsTipMapper;
import com.zzx.schoolfellow.model.entity.BmsPromotion;
import com.zzx.schoolfellow.model.entity.BmsTip;
import com.zzx.schoolfellow.service.IBmsTipService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @program: schoolfeoolw_backend
 * @description: 每日一句控制层
 * @author: zzx
 * @create: 2023-03-14 21:59
 **/
@RestController
@RequestMapping("/tip")
public class BmsTipController extends BaseController {
    @Resource
    private IBmsTipService bmsTipService;
    @Resource
    private BmsTipMapper bmsTipMapper;

    @GetMapping("/today")
    public ApiResult<BmsTip> getRandomTip() {
        BmsTip tip = bmsTipService.getRandomTip();
        return ApiResult.success(tip);
    }

    @GetMapping("/list")
    public List<BmsTip> getUserList(@RequestParam("keyword") String keyword) {
        List<BmsTip> list;
        if (Objects.equals(keyword, "")) {
            list = bmsTipService.list();
        } else {
            list = bmsTipService.list(new QueryWrapper<BmsTip>()
                    .like("content", keyword)
                    .or().like("author", keyword));
        }
        return list;
    }

    @DeleteMapping("/delete/{id}")
    public ApiResult<String> deleteTag(@PathVariable("id") String id) {
        bmsTipService.removeById(id);
        return ApiResult.success(null, "删除成功");
    }

    @PostMapping("/update")
    public ApiResult<BmsTip> updatePromote(@Valid @RequestBody BmsTip name) {
        bmsTipService.updateById(name);
        return ApiResult.success(name);
    }

    @PostMapping("/add")
    public ApiResult<BmsTip> add(@Valid @RequestBody BmsTip name) {
        bmsTipMapper.insert(name);
        return ApiResult.success(name);
    }

}

